{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "045d01ee-a22d-4ef5-bfc0-411eabc2250d",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "7e31b416-eddc-4e7e-ba30-0696f6072a29",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 创建数据集\n",
    "X =  np.random.rand(100,1)\n",
    "Y = 4 + 3*X + np.random.randn(100,1)\n",
    "X_b = np.c_[np.ones((100,1)),X]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "48242fbe-6f56-439b-94c1-634de7495127",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 创建超参数\n",
    "learning_rate = 0.001\n",
    "n_iterations = 10000"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5cad0a25-24fd-4c71-a6ec-1e4c01368d24",
   "metadata": {},
   "source": [
    "### 1、全量梯度下降"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "7d60b267-ef3a-41a2-9d62-937948fac4ec",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 初始化θ，θ1-θn\n",
    "θ = np.random.randn(2,1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6f752ceb-e58d-4f78-87f2-1113c1cd54bb",
   "metadata": {},
   "source": [
    "print(θ)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "6f3a935f-1cc8-44ec-830d-2c78a9dabed2",
   "metadata": {},
   "outputs": [],
   "source": [
    "for _ in range(n_iterations):\n",
    "    gradients = X_b.T.dot(X_b.dot(θ)-Y)\n",
    "    θ = θ - learning_rate * gradients"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "eaeeae4d-4602-4cc4-bbd6-9601a538a8cd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[4.2188102 ]\n",
      " [2.49419564]]\n"
     ]
    }
   ],
   "source": [
    "print(θ)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "63662a49-5a15-4586-80cd-344fb78cb2a7",
   "metadata": {},
   "source": [
    "### 2、随机梯度下降"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "b3ce7b1e-23a7-4d39-a46a-755a457ad69c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[-0.72631036]\n",
      " [-0.31187928]]\n"
     ]
    }
   ],
   "source": [
    "# 初始化θ，θ1-θn\n",
    "θ = np.random.randn(2,1)\n",
    "print(θ)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "6bd37571-8d25-4558-9258-711cfadafd9f",
   "metadata": {},
   "outputs": [],
   "source": [
    "for _ in range(n_iterations):\n",
    "    random_index = np.random.randint(100)\n",
    "    Xi = X_b[random_index:random_index+1]\n",
    "    Yi = Y[random_index:random_index+1]\n",
    "    gradients = Xi.T.dot(Xi.dot(θ)-Yi)\n",
    "    θ = θ - learning_rate * gradients "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "03fcae85-45c0-4916-bf48-f416aa887a0e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[4.33158321]\n",
      " [2.75038731]]\n"
     ]
    }
   ],
   "source": [
    "print(θ)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aa7ca212-735a-4224-b369-d7fd10253916",
   "metadata": {},
   "source": [
    "### 3、小批量梯度下降"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "22a5e194-69dd-4a07-9eb7-68aae1c05e41",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[-0.86427839]\n",
      " [ 0.95850261]]\n"
     ]
    }
   ],
   "source": [
    "batch_size = 10 #分组数量\n",
    "# 初始化θ，θ1-θn\n",
    "θ = np.random.randn(2,1)\n",
    "print(θ)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "5db3da34-470f-445a-8770-498e3291b86d",
   "metadata": {},
   "outputs": [],
   "source": [
    "for _ in range(n_iterations):\n",
    "    random_index = np.random.randint(100-batch_size)\n",
    "    X_batch = X_b[random_index:random_index+batch_size]\n",
    "    Y_batch = Y[random_index:random_index+batch_size]\n",
    "    gradients = X_batch.T.dot( X_batch.dot(θ)-Y_batch)\n",
    "    θ = θ - learning_rate * gradients "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "7e128b89-4379-4607-993b-a9c8912ff846",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[4.10706809]\n",
      " [3.2054323 ]]\n"
     ]
    }
   ],
   "source": [
    "print(θ)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b2226b20-eae2-446e-a7e2-2d3dc98e61c6",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
